package com.epic.quartz.service;

import com.epic.inter.IJobManager;
import com.epic.po.quartz.TaskInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class QuartzListener implements ApplicationListener<ContextRefreshedEvent> {
    private static Logger logger =  LoggerFactory.getLogger(QuartzListener.class);
//    @Autowired
//    @Resource 这个主要是注册好consumer就可以
    @DubboReference(check = false)
    private IJobManager iJobManager;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        logger.info("enter quartz");
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setRunArgs("11");
        taskInfo.setTaskId("11");
        taskInfo.setTaskBean("QuartzTask");
        taskInfo.setRunMode(1);
        taskInfo.setScheduleParam("* */5 * * * ? *");
        taskInfo.setTaskDesc("QuartzTask");
        taskInfo.setTaskUser(1);
        try{
//            iJobManager.addJob(taskInfo);
            iJobManager.test();
            logger.info("test task add paass ");
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        /**
         * init read database tbl_task_info and register 已经存在的跳过去，新增加的就添加到quartz
         * 要知道 quartz 如何删除任务 最根本的就是删除持久化的数据，重启服务 让其不再调度
         */
        logger.info("leave quartz");
    }
}
